package business

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/business"
	businessReq "github.com/flipped-aurora/gin-vue-admin/server/model/business/request"
	businessResp "github.com/flipped-aurora/gin-vue-admin/server/model/business/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"go.uber.org/zap"
)

type AnswerApi struct {
}

var answerService = service.ServiceGroupApp.BusinessServiceGroup.AnswerService

//查询企业评分回答

func (answerApi AnswerApi) QueryAnswerByEnterprise(c *gin.Context) {
	var questionnaire business.Questionnaire
	err := c.ShouldBindQuery(&questionnaire)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	//isAdmin := utils.GetUserIsAdmin(c)
	//userId := utils.GetUserID(c)
	//var userIdInfo uint
	////当不是管理员时
	//if isAdmin == 2 {
	//	userIdInfo = userId
	//}
	//查询问卷的所有题目
	questionList, err := questionService.GetQuestionInfoListByQuestionnaire(questionnaire.Uuid)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
		return
	}
	//查询问卷的所有答案
	answerList, err := answerService.QueryAnswerByEnterprise(questionnaire.Uuid)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
		return
	}
	var resultList []*businessResp.AnswerByEnterprise
	err = copier.Copy(&resultList, &questionList)
	if err != nil {
		response.FailWithMessage("查询失败", c)
		return
	}
	if len(answerList) == 0 {
		response.OkWithData(gin.H{"list": resultList}, c)
		return

	}
	//定义一个Map
	answerMap := make(map[int32][]business.Answer)
	for k, _ := range answerList {
		answerMap[answerList[k].QuestionSort] = append(answerMap[answerList[k].QuestionSort], *answerList[k])
	}

	var answerInfo []business.Answer
	markAll := 0
	for k, _ := range resultList {
		markAll = 0
		answerInfo = answerMap[resultList[k].Sort]
		for k, _ := range answerInfo {
			markAll = markAll + int(answerInfo[k].Mark)
		}

		if markAll == 0 || len(answerInfo) == 0 {
			resultList[k].Mark = 0
		} else {
			resultList[k].Mark = int32(markAll / len(answerInfo))
		}

	}
	response.OkWithData(gin.H{"list": resultList}, c)
}

// 添加回答未提交
func (answerApi AnswerApi) AddAnswerNoSubmit(c *gin.Context) {
	var answerInfo businessReq.AddAnswerSubmit
	err := c.ShouldBindJSON(&answerInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = answerService.CreatedAnswer(answerInfo.List, answerInfo.QuestionnaireUuid, answerInfo.Phone)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// 添加回答并提交
func (answerApi AnswerApi) AddAnswerSubmit(c *gin.Context) {
	var answerInfo businessReq.AddAnswerSubmit
	err := c.ShouldBindJSON(&answerInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	for k, _ := range answerInfo.List {
		answerInfo.List[k].QuestionnaireUuid = answerInfo.QuestionnaireUuid
		answerInfo.List[k].St = 1
	}
	err = answerService.CreatedAnswer(answerInfo.List)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
		return
	}
	response.OkWithMessage("创建成功", c)
}
